package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.ReturnStateEntity;
import com.person.dao.BookDao;
import com.person.dao.DBManager;
import com.person.entity.Book;

import java.util.List;

/**
 * Created by suze on 2017/6/13.
 */

public class DeleteActivity extends BaseActivity {
    private EditText id;
    private String id1;
    private Button sure;
    private JSONCallBack callBack;
    API api = API.getAPI(DeleteActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        id = (EditText) findViewById(R.id.ev_delete_book_id);
        sure = (Button) findViewById(R.id.bt_delete_book_sure);
       sure.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               id1 = id.getText().toString();
               if(id1.equals("")){
                   Toast.makeText(DeleteActivity.this,"请输入需要删除的书的ID",Toast.LENGTH_SHORT).show();
               }else {
                   callBack = new JSONCallBack<ReturnStateEntity>(new ReturnStateEntity()) {
                       @Override
                       public void onSuccess(ReturnStateEntity entity) {
                           Log.v("login", entity.getState()+" "+entity.getCode());
                           if(entity.getState().equals("success") && entity.getCode() == 0){
                               Looper.prepare();
                               Toast.makeText(DeleteActivity.this, "删除图书成功", Toast.LENGTH_LONG).show();
                               finish();
                               Looper.loop();

                           }else{
                               Looper.prepare();
                               Toast.makeText(DeleteActivity.this, "删除图书失败", Toast.LENGTH_LONG).show();
                               Looper.loop();
                           }
                       }
                   };
                   api.sqlOporate("delete", "DELETE FROM book WHERE NO="+id1, callBack);
//                   DBManager dbManager = new DBManager(DeleteActivity.this);
//                   BookDao bookDao = dbManager.getDaoSession().getBookDao();
//                   List<Book> book = bookDao.queryBuilder().where(BookDao.Properties.Id.eq(id1)).build().list();
//                   if(book.size()>0){
//                       bookDao.deleteByKey(id1);
//                       finish();
//                   }else {
//                       Toast.makeText(DeleteActivity.this,"这本书不存在~",Toast.LENGTH_SHORT).show();
//                   }
               }
           }
       });

}
}
